package javagamecontroller.activities.controllers;

import java.util.HashMap;
import util.input.android.events.AndroidButtonEvent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import com.javagamecontroller.R;


/**
 * This class creates a touch controller. As the location of the touch screen
 * makes the use of a grid button set up not practical this class contains
 * ugly custom button setup code.
 * 
 * @author Ben Schwab
 * 
 */
public class TouchController extends ControllerFrame {

    public static final String configIdentifier = "configIdentifier";
    protected static final int PRESS_VIBRATE_TIME = 13;
    protected static final int RELEASE_VIBRATE_TIME = 7;

    private HashMap<Integer, AndroidButton> myButtonImageMap;
    private HashMap<Integer, Integer> myButtonIDMap;
    private AndroidButton myXButton;
    private AndroidButton myYButton;
    private AndroidButton myBButton;
    private AndroidButton myAButton;
    private AndroidButton myStartButton;
    private AndroidButton mySelectButton;

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.touch_controller);
        initializeButtons();

    }
    //currently messy could be moved to xml in future version
    private void initializeButtons () {
        myButtonImageMap = new HashMap<Integer, AndroidButton>();
        myButtonIDMap = new HashMap<Integer, Integer>();
        myXButton = new AndroidButton(R.drawable.touch_x, R.drawable.touch_x);
        myYButton = new AndroidButton(R.drawable.touch_y, R.drawable.touch_y);
        myAButton = new AndroidButton(R.drawable.touch_a, R.drawable.touch_a);
        myBButton = new AndroidButton(R.drawable.touch_b, R.drawable.touch_b);
        myStartButton =
                new AndroidButton(R.drawable.ps_button_start_np, R.drawable.ps_button_start);
        mySelectButton =
                new AndroidButton(R.drawable.ps_button_select_np, R.drawable.ps_button_select);
        ImageView xView = (ImageView) findViewById(R.id.touch_button_one);
        xView.setBackgroundResource(R.drawable.touch_x);
        xView.setOnTouchListener(myButtonListener);
        ImageView yView = (ImageView) findViewById(R.id.touch_button_two);
        yView.setBackgroundResource(R.drawable.touch_y);
        yView.setOnTouchListener(myButtonListener);
        ImageView aView = (ImageView) findViewById(R.id.touch_button_three);
        aView.setBackgroundResource(R.drawable.touch_a);
        aView.setOnTouchListener(myButtonListener);
        ImageView bView = (ImageView) findViewById(R.id.touch_button_four);
        bView.setBackgroundResource(R.drawable.touch_b);
        bView.setOnTouchListener(myButtonListener);
        ImageView startView = (ImageView) findViewById(R.id.touch_button_start);
        startView.setBackgroundResource(R.drawable.ps_button_start_np);
        startView.setOnTouchListener(myButtonListener);
        ImageView selectView = (ImageView) findViewById(R.id.touch_button_select);
        selectView.setBackgroundResource(R.drawable.ps_button_select_np);
        selectView.setOnTouchListener(myButtonListener);
        myButtonImageMap.put(R.id.touch_button_one, myXButton);
        myButtonImageMap.put(R.id.touch_button_two, myYButton);
        myButtonImageMap.put(R.id.touch_button_three, myAButton);
        myButtonImageMap.put(R.id.touch_button_four, myBButton);
        myButtonImageMap.put(R.id.touch_button_start, myStartButton);
        myButtonImageMap.put(R.id.touch_button_select, mySelectButton);
        myButtonIDMap.put(R.id.touch_button_one, AndroidButtonEvent.TouchController.X);
        myButtonIDMap.put(R.id.touch_button_two, AndroidButtonEvent.TouchController.Y);
        myButtonIDMap.put(R.id.touch_button_three, AndroidButtonEvent.TouchController.A);
        myButtonIDMap.put(R.id.touch_button_four, AndroidButtonEvent.TouchController.B);
        myButtonIDMap.put(R.id.touch_button_start, AndroidButtonEvent.TouchController.START);
        myButtonIDMap.put(R.id.touch_button_select, AndroidButtonEvent.TouchController.SELECT);
    }

    private final OnTouchListener myButtonListener = new OnTouchListener() {

        @Override
        public boolean onTouch (View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    writeButtonPressed(v);
                    break;
                case MotionEvent.ACTION_UP:
                    writeButtonReleased(v);
                    break;
                case MotionEvent.ACTION_CANCEL:
                    writeButtonReleased(v);
                    break;
                default:
                    break;
            }
            return true;
        }

    };

    private void writeButtonReleased (View v) {
        if (myButtonIDMap.containsKey(v.getId())) {
            int code = myButtonIDMap.get(v.getId());
            int action = AndroidButtonEvent.BUTTON_RELEASED;
            AndroidButton curButton = myButtonImageMap.get(v.getId());
            curButton.setPressed(false);

            AndroidButtonEvent b2 = new AndroidButtonEvent(code, action);
            getMessenger().write(b2);
            getVibrator().vibrate(RELEASE_VIBRATE_TIME);
            ((ImageView) v).setImageResource(curButton.getDrawState());
            v.refreshDrawableState();

        }
    }

    private void writeButtonPressed (View v) {
        if (myButtonIDMap.containsKey(v.getId())) {
            int code = myButtonIDMap.get(v.getId());
            int action = AndroidButtonEvent.BUTTON_PRESSED;
            if (v.getId() == R.id.touch_button_select) {
                showNoticeDialog();
            }
            else {
                AndroidButton curButton = myButtonImageMap.get(v.getId());
                curButton.setPressed(true);
                AndroidButtonEvent b = new AndroidButtonEvent(code, action);
                getMessenger().write(b);
                getVibrator().vibrate(PRESS_VIBRATE_TIME);
                ((ImageView) v).setImageResource(curButton.getDrawState());
                v.refreshDrawableState();
            }

        }

    }
}